package com.youdianda.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.dto.ArticleDto;
import com.youdianda.dto.ArticlePageDto;
import com.youdianda.dto.ArticleQueryPageDTO;
import com.youdianda.entity.*;
import com.youdianda.mapper.*;
import com.youdianda.result.Result;
import com.youdianda.service.IYddArticleService;
import com.youdianda.service.IYddCateService;
import com.youdianda.service.IYddTagService;
import com.youdianda.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mosen
 * @since 2023-05-15
 */
@Service
public class YddArticleServiceImpl extends ServiceImpl<YddArticleMapper, YddArticle> implements IYddArticleService {

    @Autowired
    private IYddCateService yddCateService;

    /**
     * 根据文章id删除
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result<YddArticle> delArticle(String id) {
        this.removeById(id);
        return Result.success();
    }

    /**
     * 修改文章
     * @param   yddArticle
     * @return
     */
    @Transactional
    @Override
    public Result<YddArticle> updateArticle(YddArticle yddArticle) {

        this.updateById(yddArticle);
        return Result.success();
    }

    /**
     * 根据文章id查询
     * @param
     * @return
     */
    @Override
    public Result<YddArticle> ArticleById(String id) {

        YddArticle yddArticle = this.getById(id);
        return Result.success(yddArticle);
    }

    /**
     * 新增文章
     * @param
     * @return
     */
    @Transactional
    @Override
    public Result<YddArticle> addArticle(YddArticle yddArticle) {

        this.save(yddArticle);
        return Result.success();
    }

    /**
     * 文章分页查询
     * @param dto
     * @return
     */
    @Override
    public Result<ArticleQueryPageVO> articlePage(ArticleQueryPageDTO dto) {

        //1.设置分页参数
        Page<YddArticle> page = new Page<>(dto.getPage(),dto.getLimit());

        //2.用mp设置动态sql
        LambdaQueryWrapper<YddArticle> articlePage = new LambdaQueryWrapper<>();
        //2.1动态时间
        articlePage.ge(dto.getStart_time()!=null, YddArticle::getCreateDate,dto.getStart_time())
        .le(dto.getEnd_time()!=null,YddArticle::getCreateDate,dto.getEnd_time());

        //2.2动态文章名
        articlePage.like(dto.getTitle()!=null,YddArticle::getTitle,dto.getTitle());

        //2.3动态分类id
        articlePage.like(dto.getCateid()!=null,YddArticle::getCateid, dto.getCateid());

        //3.根据创建时间倒序
        articlePage.orderByDesc(YddArticle::getCreateDate);

        //4.分页查询
        Page<YddArticle> yddArticlePage = this.page(page, articlePage);

        List<ArticleAllVO> list = new ArrayList<>();

        //4.1呼应前端
        for (YddArticle record : yddArticlePage.getRecords()) {
            ArticleAllVO articleListVO = new ArticleAllVO();
            Result<YddCate> yddCateResult = yddCateService.CateById(String.valueOf(record.getCateid()));

            articleListVO.setCreate_date(record.getCreateDate());
            articleListVO.setDefault_data(record.getDefaultData());
            articleListVO.setUpdate_date(record.getUpdateDate());
            articleListVO.setUser_id(record.getUserId());

            if (yddCateResult.getData()==null) {
                articleListVO.setCatename("该分类已删除");
            }else {
                articleListVO.setCatename(yddCateResult.getData().getCatename());
            }
            BeanUtils.copyProperties(record,articleListVO);

            list.add(articleListVO);
        }
        BeanUtils.copyProperties(yddArticlePage.getRecords(),list);
        //5.封装vo
        ArticleQueryPageVO articleQueryPageVO = new ArticleQueryPageVO(dto.getPage(), dto.getLimit(), yddArticlePage.getTotal(), yddArticlePage.getPages(), list);
        return Result.success(articleQueryPageVO);
    }

    @Autowired
    private YddArticleMapper yddArticleMapper;
    @Autowired
    private YddCateMapper yddCateMapper;
    @Autowired
    private YddTagMapper yddTagMapper;
    @Autowired
    private YddLinkMapper yddLinkMapper;
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;


    /**
     * 推荐文章
     * @param articlePageDto
     * @return
     */
    @Override
    public YddArticleVo getArticle(ArticlePageDto articlePageDto) {
        YddArticleVo yddArticleVo=new YddArticleVo();

        // 创建IPage分页对象,设置分页参数
        IPage<YddArticle> page=new Page<>(articlePageDto.getPage(),articlePageDto.getLimit());
        IPage<YddArticle> yddArticleIPage = yddArticleMapper.selectPage(page, null);

        //获取符合条件的数据
        List<ArticleVo> hot = yddArticleMapper.getHot();
        //判断标签是否为空
        for (ArticleVo articleVo : hot) {
            if (articleVo.getTags()==null){
                articleVo.setTags("");

            }
        }

        ArticlePageVo articlePageVo = new ArticlePageVo();
        //查询所有的创建时间
        List<YddArticle> createCount = yddArticleMapper.getCreateCount();
        //定义一天以内的文章数量为0
        int numDay=0;
        for (YddArticle yddArticle : createCount) {
            //获取当前时间戳
            long now = System.currentTimeMillis();
            //判断如何现在时间戳减去创建时间戳小于等于24小时时间戳那么一天以内文章数量加1
            if (now- yddArticle.getCreateDate()<=86400000){
                numDay++;
            }
        }
        //定义一周以内的文章数量为0
        int numWeek=0;
        for (YddArticle yddArticle : createCount) {
            //获取当前时间戳
            long now = System.currentTimeMillis();
            //判断如何现在时间戳减去创建时间戳小于等于一周时间戳那么一天以内文章数量加1
            if (now-yddArticle.getCreateDate()<=604800000){
                numWeek++;
            }
        }
        //给分页数据赋值
        int total = (int) yddArticleIPage.getTotal();
        int pages = (int)yddArticleIPage.getPages();
        int size = (int)yddArticleIPage.getSize();
        int current = (int)yddArticleIPage.getCurrent();
        ArticlePageVo pageVo = articlePageVo;
        pageVo.setCount(hot.size());
        pageVo.setPageSize(size);
        pageVo.setCurrentPage(current);
        pageVo.setTotalPages(pages);
        pageVo.setData(hot);

        //给返回类型赋值
        yddArticleVo.setDesc("首页大列表");
        yddArticleVo.setList(pageVo);
        yddArticleVo.setNum_day(numDay);
        yddArticleVo.setNum_week(numWeek);
        return yddArticleVo;
    }

    /**
     * 查询文章列表
     * @param articleDto
     * @return
     */
    @Override
    public ListVo getArticleList(ArticleDto articleDto) {
        // 创建IPage分页对象,设置分页参数
        Page<YddArticle> articlePageVoPage = new Page<>(articleDto.getPage(), articleDto.getLimit());
        List<ArticleVo> articleVo= yddArticleMapper.findBypage(articlePageVoPage,articleDto.getCateid());
        ListVo listVo = new ListVo();
        ArticlePageVo pageVo = new ArticlePageVo();
        //给分页数据赋值
        int total = (int) articlePageVoPage.getTotal();
        int pages = (int) articlePageVoPage.getPages();
        int size = (int)articlePageVoPage.getSize();
        int current = (int)articlePageVoPage.getCurrent();
        pageVo.setCount((int) articlePageVoPage.getSize());
        pageVo.setPageSize(size);
        pageVo.setCurrentPage(current);
        pageVo.setTotalPages(pages);
        pageVo.setData(articleVo);
        //返回结果
        listVo.setList(pageVo);
        return listVo ;



    }

    /**
     * 查看推荐文章
     */

    @Override
    public YddArticleHotVo getNew() {
        //查找符合条件的数据
        List<ArticleHotVo> articleHot = yddArticleMapper.getArticleNew();
        //放在对应的实体类
        YddArticleHotVo yddArticleHotVo = new YddArticleHotVo();
        ArrayList<HotVo> hotVos = new ArrayList<>();

        for (ArticleHotVo articleHotVo : articleHot) {
            HotVo hotVo = new HotVo();
            hotVo.setClick(articleHotVo.getClick());
            hotVo.setLovenum(articleHotVo.getLovenum());
            hotVo.setCatename(articleHotVo.getCatename());
            hotVo.setTitle(articleHotVo.getTitle());
            hotVo.setPic(articleHotVo.getPic());
            hotVo.setId(articleHotVo.getId());
            hotVos.add(hotVo);
        }
        yddArticleHotVo.setList(hotVos);
        yddArticleHotVo.setDesc("侧边最新文章");
        //返回结果
        return yddArticleHotVo;


    }

    /**
     * 查看推荐文章
     * @return
     */
    @Override
    public YddArticleHotVo getArticleHot() {
        List<ArticleHotVo> articleHot = yddArticleMapper.getArticleHot();
        YddArticleHotVo yddArticleHotVo = new YddArticleHotVo();
        ArrayList<HotVo> hotVos = new ArrayList<>();
        for (ArticleHotVo articleHotVo : articleHot) {
            HotVo hotVo = new HotVo();
            hotVo.setClick(articleHotVo.getClick());
            hotVo.setLovenum(articleHotVo.getLovenum());
            hotVo.setCatename(articleHotVo.getCatename());
            hotVo.setTitle(articleHotVo.getTitle());
            hotVo.setPic(articleHotVo.getPic());
            hotVo.setId(articleHotVo.getId());
            hotVos.add(hotVo);
        }
        ////放在对应的实体类

        yddArticleHotVo.setDesc("侧边推荐文章");
        yddArticleHotVo.setList(hotVos);

        return yddArticleHotVo;
    }

    /**
     * 查看首页和全局数据
     * @return
     */
    @Override
    public ArticleIndexVo getIndex(){
        //创建最后返回值对象
        ArticleIndexVo articleIndexVo = new ArticleIndexVo();
        //查询所有分类 传入到对应的实体类中
        List<YddCate> yddCates = yddCateMapper.selectList(null);
        List<CateVo> cateVo1 = new ArrayList<>();
        for (YddCate yddCate : yddCates) {
            CateVo cateVo = new CateVo();
            cateVo.setCatename(yddCate.getCatename());
            cateVo.setIcon(yddCate.getIcon());
            cateVo.setSort_num(yddCate.getSortNum());
            cateVo.setId(yddCate.getId());
            cateVo.setDefault_data(yddCate.getDefaultData());
            cateVo1.add(cateVo);
        }
        articleIndexVo.setAllCate(cateVo1);
        //查询所有标签 传入到对应的实体类中
        List<YddTag> yddTags = yddTagMapper.selectList(null);
        List<TagVo> tagVos = new ArrayList<>();
        for (YddTag yddTag : yddTags) {
            TagVo tagVo = new TagVo();
            tagVo.setTagname(yddTag.getTagname());
            tagVo.setId(yddTag.getId());
            tagVo.setDefault_data(yddTag.getDefaultData());
            tagVos.add(tagVo);
        }

        articleIndexVo.setAllTag(tagVos);


        //查询所有所有友情链接 传入到对应的实体类中
        List<YddLink> yddLinks = yddLinkMapper.selectList(null);
        List<LinkVo> linkVos = new ArrayList<>();
        for (YddLink yddLink : yddLinks) {
            LinkVo linkVo = new LinkVo();
            linkVo.setId(yddLink.getId());
            linkVo.setDes(yddLink.getDes());
            linkVo.setImg(yddLink.getImg());
            linkVo.setDefault_data(yddLink.getDefaultData());
            linkVo.setTitle(yddLink.getTitle());
            linkVo.setType(yddLink.getType());
            linkVo.setUrl(yddLink.getUrl());
            linkVos.add(linkVo);
        }
        articleIndexVo.setAllLink(linkVos);
        //查询首页轮播图
        List<IndexBannerVO> banner = yddAdvimgMapper.Banner();
        articleIndexVo.setBanner(banner);
        //查询详情广告页 传入到对应的实体类中
        List<IndexBannerVO> showBanner=yddAdvimgMapper.getShowBanner();
        articleIndexVo.setShowBanner(showBanner);
        //侧边栏图片 传入到对应的实体类中
        List<IndexBannerVO> sideBanner=yddAdvimgMapper.getSideBanner();
        articleIndexVo.setSideBanner(sideBanner);


        //获取一部分的字段sys_config数据 并放入对应的实体类
        List<SysConfig> configs = sysConfigMapper.selectList(null);
        SysConfigVO sysConfigVo = new SysConfigVO();
        List<AllDataVo> allDataVos = new ArrayList<>();
        for (SysConfig config : configs) {
            AllDataVo allDataVo = new AllDataVo();
            allDataVo.setType(config.getType());
            allDataVo.setId(config.getId());
            allDataVo.setValidator(config.getValidator());
            allDataVo.setAttr_key(config.getAttrKey());
            allDataVo.setAttr_value(config.getAttrValue());
            allDataVo.setConfig_value(config.getConfigValue());
            allDataVos.add(allDataVo);
        }
        sysConfigVo.setAllData(allDataVos);
        articleIndexVo.setConfig(sysConfigVo);
        return articleIndexVo;


    }

    @Override
    public ShowVo getShow(Integer id) {
        LambdaQueryWrapper<YddArticle> lqw = new LambdaQueryWrapper();
        lqw.eq(YddArticle::getId,id);
        List<YddArticle> yddArticles = yddArticleMapper.selectList(lqw);
        ShowVo showVo = new ShowVo();
        ShowArticleVo showArticleVo = new ShowArticleVo();
        IdVo next = new IdVo();
        IdVo prev = new IdVo();
        List<RecommendVo> recommendVos = new ArrayList<>();
        RecommendVo recommendVo = new RecommendVo();

        for (YddArticle yddArticle : yddArticles) {
            showArticleVo.setAuthor(yddArticle.getAuthor());
            showArticleVo.setCateid(yddArticle.getCateid());
            showArticleVo.setClick(yddArticle.getClick());
            showArticleVo.setContent(yddArticle.getContent());
            showArticleVo.setCreate_date(yddArticle.getCreateDate());
            showArticleVo.setDescription(yddArticle.getDescription());
            showArticleVo.setDefault_data(yddArticle.getDefaultData());
            showArticleVo.setId(yddArticle.getId());
            showArticleVo.setIshot(yddArticle.getIshot());
            showArticleVo.setIsCollect("false");
            showArticleVo.setIsLike("false");
            showArticleVo.setIstop(yddArticle.getIstop());
            showArticleVo.setKeywords(yddArticle.getKeywords());
            showArticleVo.setLovenum(yddArticle.getLovenum());
            showArticleVo.setUser_id(yddArticle.getUserId());
            showArticleVo.setStatus(yddArticle.getStatus());
            showArticleVo.setTags(yddArticle.getTags());
            showArticleVo.setPic(yddArticle.getPic());
            showArticleVo.setTitle(yddArticle.getTitle());
            showArticleVo.setUpdate_date(yddArticle.getUpdateDate());
            showVo.setInfo(showArticleVo);
            next.setId(yddArticle.getId()+1);
            prev.setId(yddArticle.getId()-1);
            recommendVo.setCateid(yddArticle.getCateid());
        }
        //查询文章上一页
        List<IdVo>  nextVo =yddArticleMapper.selectNextIdVo(next.getId());
        //查询文章下一页
        List<IdVo> prevVo =yddArticleMapper.selectPrevIdVo(prev.getId());

        //查询文章的部分数据
        recommendVos=  yddArticleMapper.selectRecommend(recommendVo.getCateid());

        showVo.setNext(nextVo);
        showVo.setPrev(prevVo);
        showVo.setRecommend(recommendVos);
        return showVo;

    }
}
